package mi.payment.transaction.balance;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import mi.payment.Constant;
import mi.payment.R;
import mi.payment.bean.db.TradeData;
import mi.payment.db.dao.PayDetail;
import mi.payment.transaction.V2InputPasswordActivity;
import mi.payment.transaction.ResultActivity;
import mi.payment.utils.transaction.BalanceQueryTask;
import sunmi.payment.ui.dialog.LoadDialog;

/**
 * 余额模块 -- 输入密码界面
 *
 * @author KenMa on 2017/1/8.
 */
public class BalanceInputPasswordActivity extends V2InputPasswordActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        startTransactionProcess();
    }

    private void initView() {
        mTvTitle.setText(R.string.menu_query_balance);

        showCardNoView(true);
        showAmountView(false);
    }

    @Override
    @SuppressLint("StaticFieldLeak")
    public void onStartOnline() {
        // 余额查询
        mTradeData.getPayDetail().setTransactionType(12);
        BalanceQueryTask task = new BalanceQueryTask(this) {

            @Override
            protected void onPostExecuteImpl(TradeData tradeData) {
                PayDetail resPay = tradeData.getPayDetail();
                boolean equals = TextUtils.equals(resPay.tradeAnswerCode, "00");
                if (equals) {
                    BalanceResultActivity.startAction(mActivity, resPay.CardNo, resPay.additionalAmount, resPay);
                } else {
                    ResultActivity.startAction(mActivity, R.string.menu_query_balance, resPay);
                }
                finish();
            }

            @Override
            protected void onTaskTimeoutImpl() {
                mTradeData.payDetail.tradeAnswerCode = Constant.ANSWER_CODE_TIMEOUT;
                ResultActivity.startAction(mActivity, R.string.menu_query_balance, mTradeData.payDetail);
                finish();
            }

        };
        LoadDialog dialog = getLoadingDialog(R.string.load_connecting);
        task.setLoadDialog(dialog);
        task.execute(mTradeData);
    }

    public static void startAction(Context context) {
        Intent intent = new Intent(context, BalanceInputPasswordActivity.class);
        context.startActivity(intent);
    }


}
